| 1 | /** |
| 2 | <Trolly is a simple shopping list application for android phones.> |
| 3 | Copyright (C) 2009 Ben Caldwell |
| 4 | |
| 5 | This program is free software: you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License as published by |
| 7 | the Free Software Foundation, either version 3 of the License, or |
| 8 | (at your option) any later version. |
| 9 | |
| 10 | This program is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | |
| 20 | package caldwell.ben.trolly; |
| 21 | |
| 22 | import java.util.ArrayList; |
| 23 | |
| 24 | import caldwell.ben.provider.Trolly.ShoppingList; |
| 25 | import android.app.AlertDialog; |
| 26 | import android.app.Dialog; |
| 27 | import android.app.ListActivity; |
| 28 | import android.content.ContentResolver; |
| 29 | import android.content.ContentUris; |
| 30 | import android.content.ContentValues; |
| 31 | import android.content.Context; |
| 32 | import android.content.DialogInterface; |
| 33 | import android.content.Intent; |
| 34 | import android.content.SharedPreferences; |
| 35 | import android.database.Cursor; |
| 36 | import android.graphics.Color; |
| 37 | import android.graphics.Paint; |
| 38 | import android.net.Uri; |
| 39 | import android.os.Bundle; |
| 40 | import android.preference.PreferenceManager; |
| 41 | import android.view.ContextMenu; |
| 42 | import android.view.LayoutInflater; |
| 43 | import android.view.Menu; |
| 44 | import android.view.MenuItem; |
| 45 | import android.view.View; |
| 46 | import android.view.ViewGroup; |
| 47 | import android.view.ContextMenu.ContextMenuInfo; |
| 48 | import android.widget.AdapterView; |
| 49 | import android.widget.AutoCompleteTextView; |
| 50 | import android.widget.Button; |
| 51 | import android.widget.CursorAdapter; |
| 52 | import android.widget.EditText; |
| 53 | import android.widget.Filterable; |
| 54 | import android.widget.ListView; |
| 55 | import android.widget.SimpleCursorAdapter; |
| 56 | import android.widget.TextView; |
| 57 | |
| 58 | public class Trolly extends ListActivity { |
| 59 | |
| 60 | // private static final String TAG = "Trolly"; |
| 61 | |
| 62 | public static final String KEY_ITEM = "items"; |
| 63 | public static boolean adding = false; |
| 64 | |
| 65 | /** |
| 66 | * TrollyAdapter allows crossing items off the list and filtering |
| 67 | * on user text input. |
| 68 | * @author Ben |
| 69 | * |
| 70 | */ |
| 71 | private static class TrollyAdapter extends SimpleCursorAdapter implements Filterable { |
| 72 | |
| 73 | private ContentResolver mContent; |
| 74 | |
| 75 | public TrollyAdapter(Context context, int layout, Cursor c, |
| 76 | String[] from, int[] to) { |
| 77 | super(context, layout, c, from, to); |
| 78 | mContent = context.getContentResolver(); |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public Cursor runQueryOnBackgroundThread(CharSequence constraint) { |
| 83 | if (getFilterQueryProvider() != null) { |
| 84 | return getFilterQueryProvider().runQuery(constraint); |
| 85 | } |
| 86 | |
| 87 | StringBuilder buffer = null; |
| 88 | String[] args = null; |
| 89 | if (constraint != null) { |
| 90 | buffer = new StringBuilder(); |
| 91 | buffer.append("UPPER("); |
| 92 | buffer.append(ShoppingList.ITEM); |
| 93 | buffer.append(") GLOB ?"); |
| 94 | args = new String[] { "*" + constraint.toString().toUpperCase() + "*" }; |
| 95 | } |
| 96 | |
| 97 | return mContent.query(ShoppingList.CONTENT_URI, PROJECTION, |
| 98 | buffer == null ? null : buffer.toString(), args, |
| 99 | null); |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public void bindView(View view, Context context, Cursor cursor) { |
| 104 | TextView item = (TextView)view.findViewById(R.id.item); |
| 105 | item.setText(cursor.getString(cursor.getColumnIndex(ShoppingList.ITEM))); |
| 106 | switch(cursor.getInt(cursor.getColumnIndex(ShoppingList.STATUS))){ |
| 107 | case ShoppingList.OFF_LIST: |
| 108 | item.setPaintFlags(item.getPaintFlags() & ~Paint.STRIKE_THRU_TEXT_FLAG); |
| 109 | item.setTextColor(Color.DKGRAY); |
| 110 | break; |
| 111 | case ShoppingList.ON_LIST: |
| 112 | item.setPaintFlags(item.getPaintFlags() & ~Paint.STRIKE_THRU_TEXT_FLAG); |
| 113 | item.setTextColor(Color.GREEN); |
| 114 | break; |
| 115 | case ShoppingList.IN_TROLLEY: |
| 116 | item.setPaintFlags(item.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG); |
| 117 | item.setTextColor(Color.GRAY); |
| 118 | break; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public CharSequence convertToString(Cursor cursor) { |
| 124 | return cursor.getString(cursor.getColumnIndex(ShoppingList.ITEM)); |
| 125 | } |
| 126 | |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * AutoFillAdapter is an adapter for the AutoCompleteTextView at the top of the Trolly Activity |
| 131 | * @author Ben Caldwel |
| 132 | * |
| 133 | */ |
| 134 | private static class AutoFillAdapter extends CursorAdapter implements Filterable { |
| 135 | |
| 136 | private ContentResolver mContent; |
| 137 | |
| 138 | public AutoFillAdapter(Context context, Cursor c) { |
| 139 | super(context, c); |
| 140 | mContent = context.getContentResolver(); |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public void bindView(View view, Context context, Cursor cursor) { |
| 145 | ((TextView) view).setText(cursor.getString(cursor.getColumnIndex(ShoppingList.ITEM))); |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public View newView(Context context, Cursor cursor, ViewGroup parent) { |
| 150 | final LayoutInflater inflater = LayoutInflater.from(context); |
| 151 | final TextView view = (TextView)inflater.inflate( |
| 152 | android.R.layout.simple_dropdown_item_1line, |
| 153 | parent,false); |
| 154 | view.setText(cursor.getString(cursor.getColumnIndex(ShoppingList.ITEM))); |
| 155 | return view; |
| 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public CharSequence convertToString(Cursor cursor) { |
| 160 | return cursor.getString(cursor.getColumnIndex(ShoppingList.ITEM)); |
| 161 | } |
| 162 | |
| 163 | @Override |
| 164 | public Cursor runQueryOnBackgroundThread(CharSequence constraint) { |
| 165 | if (getFilterQueryProvider() != null) { |
| 166 | return getFilterQueryProvider().runQuery(constraint); |
| 167 | } |
| 168 | |
| 169 | StringBuilder buffer = null; |
| 170 | String[] args = null; |
| 171 | if (constraint != null) { |
| 172 | buffer = new StringBuilder(); |
| 173 | buffer.append("UPPER("); |
| 174 | buffer.append(ShoppingList.ITEM); |
| 175 | buffer.append(") GLOB ?"); |
| 176 | args = new String[] { "*" + constraint.toString().toUpperCase() + "*" }; |
| 177 | } |
| 178 | |
| 179 | return mContent.query(ShoppingList.CONTENT_URI, PROJECTION, |
| 180 | buffer == null ? null : buffer.toString(), args, |
| 181 | null); |
| 182 | } |
| 183 | } |
| 184 | /** |
| 185 | * The columns we are interested in from the database |
| 186 | */ |
| 187 | private static final String[] PROJECTION = new String[] { |
| 188 | ShoppingList._ID, // 0 |
| 189 | ShoppingList.ITEM, // 1 |
| 190 | ShoppingList.STATUS, // 2 |
| 191 | }; |
| 192 | |
| 193 | // Menu item ids |
| 194 | public static final int MENU_ITEM_DELETE = Menu.FIRST; |
| 195 | public static final int MENU_ITEM_INSERT = Menu.FIRST + 1; |
| 196 | public static final int MENU_ITEM_CHECKOUT = Menu.FIRST + 2; |
| 197 | public static final int MENU_ITEM_PREFERENCE = Menu.FIRST + 3; |
| 198 | public static final int MENU_ITEM_ON_LIST = Menu.FIRST + 4; |
| 199 | public static final int MENU_ITEM_OFF_LIST = Menu.FIRST + 5; |
| 200 | public static final int MENU_ITEM_IN_TROLLEY = Menu.FIRST + 6; |
| 201 | public static final int MENU_ITEM_EDIT = Menu.FIRST + 7; |
| 202 | public static final int MENU_ITEM_CLEAR = Menu.FIRST + 8; |
| 203 | public static final int MENU_ITEM_RESET = Menu.FIRST + 9; |
| 204 | |
| 205 | /** |
| 206 | * Case selections for the type of dialog box displayed |
| 207 | */ |
| 208 | private static final int DIALOG_DELETE = 1; |
| 209 | private static final int DIALOG_EDIT = 2; |
| 210 | private static final int DIALOG_CLEAR = 3; |
| 211 | private static final int DIALOG_RESET = 4; |
| 212 | |
| 213 | //Use private members for dialog textview to prevent weird persistence problem |
| 214 | private EditText mDialogEdit; |
| 215 | private TextView mDialogText; |
| 216 | private View mDialogView; |
| 217 | |
| 218 | private Cursor mCursor; |
| 219 | private AutoCompleteTextView mTextBox; |
| 220 | private Button btnAdd; |
| 221 | private TrollyAdapter mAdapter; |
| 222 | private SharedPreferences mPrefs; |
| 223 | |
| 224 | private Uri mUri; |
| 225 | |
| 226 | |
| 227 | /** Called when the activity is first created. */ |
| 228 | @Override |
| 229 | public void onCreate(Bundle savedInstanceState) { |
| 230 | super.onCreate(savedInstanceState); |
| 231 | |
| 232 | // If no data was given in the intent (because we were started |
| 233 | // as a MAIN activity), then use our default content provider. |
| 234 | Intent intent = getIntent(); |
| 235 | if (intent.getData() == null) { |
| 236 | intent.setData(ShoppingList.CONTENT_URI); |
| 237 | } |
| 238 | |
| 239 | setContentView(R.layout.trolly); |
| 240 | // Inform the list we provide context menus for items |
| 241 | getListView().setOnCreateContextMenuListener(this); |
| 242 | |
| 243 | adding = false; |
| 244 | updateList(); |
| 245 | |
| 246 | mTextBox = (AutoCompleteTextView)findViewById(R.id.textbox); |
| 247 | btnAdd = (Button)findViewById(R.id.btn_add); |
| 248 | |
| 249 | mTextBox.setOnClickListener(new Button.OnClickListener(){ |
| 250 | @Override |
| 251 | public void onClick(View view) { |
| 252 | //If the text box is clicked while full-list adding, stop adding |
| 253 | if (adding) { |
| 254 | adding = false; |
| 255 | updateList(); |
| 256 | } |
| 257 | } |
| 258 | }); |
| 259 | |
| 260 | btnAdd.setOnClickListener(new Button.OnClickListener(){ |
| 261 | @Override |
| 262 | public void onClick(View view) { |
| 263 | //If there is a string in the textbox then add it to the list |
| 264 | if (mTextBox.getText().length()>0) { |
| 265 | Cursor c = getContentResolver().query(getIntent().getData(), |
| 266 | PROJECTION, |
| 267 | ShoppingList.ITEM+"='"+mTextBox.getText()+"'", |
| 268 | null, |
| 269 | null); |
| 270 | c.moveToFirst(); |
| 271 | if (c == null |
| 272 | || c.isBeforeFirst() |
| 273 | || c.getInt(c.getColumnIndex(ShoppingList.STATUS))==ShoppingList.ON_LIST) { |
| 274 | ContentValues values = new ContentValues(); |
| 275 | values.put(ShoppingList.ITEM, mTextBox.getText().toString()); |
| 276 | getContentResolver().insert(ShoppingList.CONTENT_URI,values); |
| 277 | } else { |
| 278 | ContentValues values = new ContentValues(); |
| 279 | values.put(ShoppingList.STATUS, ShoppingList.ON_LIST); |
| 280 | long id = c.getLong(c.getColumnIndex(ShoppingList._ID)); |
| 281 | Uri uri = ContentUris.withAppendedId(getIntent().getData(), id); |
| 282 | getContentResolver().update(uri, values, null, null); |
| 283 | } |
| 284 | mTextBox.setText(""); |
| 285 | } else { |
| 286 | adding = !adding; |
| 287 | updateList(); |
| 288 | } |
| 289 | } |
| 290 | }); |
| 291 | //mPrefs = getSharedPreferences(null, MODE_PRIVATE); |
| 292 | mPrefs = PreferenceManager.getDefaultSharedPreferences(this); |
| 293 | } |
| 294 | |
| 295 | protected void updateList() { |
| 296 | //set up the list cursor |
| 297 | mCursor = managedQuery(getIntent().getData(), |
| 298 | PROJECTION, |
| 299 | adding ? null: ShoppingList.STATUS+"<>"+ShoppingList.OFF_LIST, |
| 300 | null, |
| 301 | ShoppingList.DEFAULT_SORT_ORDER); |
| 302 | |
| 303 | //set the list adapter |
| 304 | mAdapter = new TrollyAdapter(this, R.layout.shoppinglist_item, mCursor, |
| 305 | new String[] { ShoppingList.ITEM}, new int[] { R.id.item}); |
| 306 | setListAdapter(mAdapter); |
| 307 | } |
| 308 | |
| 309 | @Override |
| 310 | protected void onResume() { |
| 311 | super.onResume(); |
| 312 | |
| 313 | adding = false; |
| 314 | updateList(); |
| 315 | |
| 316 | Cursor cAutoFill = managedQuery(getIntent().getData(), |
| 317 | PROJECTION, |
| 318 | null, |
| 319 | null, |
| 320 | ShoppingList.DEFAULT_SORT_ORDER); |
| 321 | |
| 322 | AutoFillAdapter autoFillAdapter = new AutoFillAdapter(this, cAutoFill); |
| 323 | mTextBox.setAdapter(autoFillAdapter); |
| 324 | } |
| 325 | |
| 326 | @Override |
| 327 | protected void onPause() { |
| 328 | super.onPause(); |
| 329 | SharedPreferences.Editor ed = mPrefs.edit(); |
| 330 | ed.commit(); |
| 331 | } |
| 332 | |
| 333 | @Override |
| 334 | protected void onListItemClick(ListView l, View v, int position, long id) { |
| 335 | //super.onListItemClick(l, v, position, id); |
| 336 | Uri uri = ContentUris.withAppendedId(getIntent().getData(), id); |
| 337 | Cursor c = getContentResolver().query(uri, PROJECTION, null, null, null); |
| 338 | c.moveToFirst(); |
| 339 | ContentValues values = new ContentValues(); |
| 340 | switch (c.getInt(c.getColumnIndex(ShoppingList.STATUS))) |
| 341 | { |
| 342 | case ShoppingList.OFF_LIST: |
| 343 | //move from off the list to on the list |
| 344 | values.put(ShoppingList.STATUS, ShoppingList.ON_LIST); |
| 345 | getContentResolver().update(uri, values, null, null); |
| 346 | break; |
| 347 | case ShoppingList.ON_LIST: |
| 348 | values.put(ShoppingList.STATUS, ShoppingList.IN_TROLLEY); |
| 349 | getContentResolver().update(uri, values, null, null); |
| 350 | break; |
| 351 | case ShoppingList.IN_TROLLEY: |
| 352 | //move back from in the trolley to on the list |
| 353 | values.put(ShoppingList.STATUS, ShoppingList.ON_LIST); |
| 354 | getContentResolver().update(uri, values, null, null); |
| 355 | break; |
| 356 | } |
| 357 | if (adding) { |
| 358 | adding = false; |
| 359 | updateList(); |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | @Override |
| 364 | public boolean onContextItemSelected(MenuItem item) { |
| 365 | AdapterView.AdapterContextMenuInfo info; |
| 366 | try { |
| 367 | info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo(); |
| 368 | } catch (ClassCastException e) { |
| 369 | return false; |
| 370 | } |
| 371 | |
| 372 | Cursor cursor = (Cursor) getListAdapter().getItem(info.position); |
| 373 | if (cursor == null) { |
| 374 | // For some reason the requested item isn't available, do nothing |
| 375 | return false; |
| 376 | } |
| 377 | |
| 378 | mUri = ContentUris.withAppendedId(getIntent().getData(), |
| 379 | cursor.getLong(cursor.getColumnIndex(ShoppingList._ID))); |
| 380 | Cursor c = getContentResolver().query(mUri, PROJECTION, null, null, null); |
| 381 | c.moveToFirst(); |
| 382 | ContentValues values = new ContentValues(); |
| 383 | |
| 384 | switch (item.getItemId()) { |
| 385 | case MENU_ITEM_ON_LIST: |
| 386 | // Change to "on list" status |
| 387 | values.put(ShoppingList.STATUS, ShoppingList.ON_LIST); |
| 388 | getContentResolver().update(mUri, values, null, null); |
| 389 | return true; |
| 390 | case MENU_ITEM_OFF_LIST: |
| 391 | // Change to "off list" status |
| 392 | values.put(ShoppingList.STATUS, ShoppingList.OFF_LIST); |
| 393 | getContentResolver().update(mUri, values, null, null); |
| 394 | return true; |
| 395 | case MENU_ITEM_IN_TROLLEY: |
| 396 | //Change to "in trolley" status |
| 397 | values.put(ShoppingList.STATUS, ShoppingList.IN_TROLLEY); |
| 398 | getContentResolver().update(mUri, values, null, null); |
| 399 | return true; |
| 400 | case MENU_ITEM_EDIT: |
| 401 | //Show edit dialog |
| 402 | showDialog(DIALOG_EDIT); |
| 403 | mDialogEdit.setText(c.getString(c.getColumnIndex(ShoppingList.ITEM))); |
| 404 | return true; |
| 405 | case MENU_ITEM_DELETE: |
| 406 | //Show are you sure dialog then delete |
| 407 | showDialog(DIALOG_DELETE); |
| 408 | mDialogText.setText(c.getString(c.getColumnIndex(ShoppingList.ITEM))); |
| 409 | return true; |
| 410 | } |
| 411 | return false; |
| 412 | } |
| 413 | |
| 414 | @Override |
| 415 | public void onCreateContextMenu(ContextMenu menu, View v, |
| 416 | ContextMenuInfo menuInfo) { |
| 417 | AdapterView.AdapterContextMenuInfo info; |
| 418 | try { |
| 419 | info = (AdapterView.AdapterContextMenuInfo) menuInfo; |
| 420 | } catch (ClassCastException e) { |
| 421 | return; |
| 422 | } |
| 423 | Cursor cursor = (Cursor)getListAdapter().getItem(info.position); |
| 424 | if (cursor == null) { |
| 425 | // For some reason the requested item isn't available, do nothing |
| 426 | return; |
| 427 | } |
| 428 | // Setup the menu header |
| 429 | menu.setHeaderTitle(cursor.getString(cursor.getColumnIndex(ShoppingList.ITEM))); |
| 430 | int status = cursor.getInt(cursor.getColumnIndex(ShoppingList.STATUS)); |
| 431 | |
| 432 | //Add context menu items depending on current state |
| 433 | switch (status) { |
| 434 | case ShoppingList.OFF_LIST: |
| 435 | menu.add(0, MENU_ITEM_ON_LIST, 0, R.string.move_on_list); |
| 436 | menu.add(0, MENU_ITEM_IN_TROLLEY, 0, R.string.move_in_trolley); |
| 437 | break; |
| 438 | case ShoppingList.ON_LIST: |
| 439 | menu.add(0, MENU_ITEM_IN_TROLLEY, 0, R.string.move_in_trolley); |
| 440 | menu.add(0, MENU_ITEM_OFF_LIST, 0, R.string.move_off_list); |
| 441 | break; |
| 442 | case ShoppingList.IN_TROLLEY: |
| 443 | menu.add(0, MENU_ITEM_ON_LIST, 0, R.string.move_on_list); |
| 444 | menu.add(0, MENU_ITEM_OFF_LIST, 0, R.string.move_off_list); |
| 445 | break; |
| 446 | } |
| 447 | |
| 448 | // Add context menu items that are relevant for all items |
| 449 | menu.add(0, MENU_ITEM_EDIT, 0, R.string.edit_item); |
| 450 | menu.add(0, MENU_ITEM_DELETE, 0, R.string.delete_item); |
| 451 | } |
| 452 | |
| 453 | @Override |
| 454 | public boolean onCreateOptionsMenu(Menu menu) { |
| 455 | super.onCreateOptionsMenu(menu); |
| 456 | menu.add(0, MENU_ITEM_CHECKOUT, 2, R.string.checkout) |
| 457 | .setIcon(android.R.drawable.ic_media_next); |
| 458 | menu.add(0, MENU_ITEM_CLEAR, 3, R.string.clear_list) |
| 459 | .setIcon(android.R.drawable.ic_menu_revert); |
| 460 | menu.add (0, MENU_ITEM_PREFERENCE, 4, R.string.preferences) |
| 461 | .setIcon(android.R.drawable.ic_menu_preferences); |
| 462 | menu.add(0, MENU_ITEM_RESET, 5, R.string.reset_list) |
| 463 | .setIcon(android.R.drawable.ic_menu_delete); |
| 464 | return true; |
| 465 | } |
| 466 | |
| 467 | @Override |
| 468 | public boolean onOptionsItemSelected(MenuItem item) { |
| 469 | switch (item.getItemId()) { |
| 470 | case MENU_ITEM_CHECKOUT: |
| 471 | //Change all items from in trolley to off list |
| 472 | checkout(); |
| 473 | return true; |
| 474 | case MENU_ITEM_CLEAR: |
| 475 | //Change all items to off list |
| 476 | showDialog(DIALOG_CLEAR); |
| 477 | mDialogText.setText(R.string.clear_prompt); |
| 478 | return true; |
| 479 | case MENU_ITEM_RESET: |
| 480 | //Change all items to off list |
| 481 | showDialog(DIALOG_RESET); |
| 482 | mDialogText.setText(R.string.reset_prompt); |
| 483 | return true; |
| 484 | case MENU_ITEM_PREFERENCE: |
| 485 | startActivity(new Intent(this,TrollyPreferences.class)); |
| 486 | return true; |
| 487 | } |
| 488 | return super.onOptionsItemSelected(item); |
| 489 | } |
| 490 | |
| 491 | @Override |
| 492 | protected Dialog onCreateDialog(int id) { |
| 493 | LayoutInflater factory = LayoutInflater.from(this); |
| 494 | switch (id) { |
| 495 | case DIALOG_EDIT: |
| 496 | mDialogView = factory.inflate(R.layout.dialog_edit, null); |
| 497 | mDialogEdit = (EditText)mDialogView.findViewById(R.id.edit); |
| 498 | return new AlertDialog.Builder(this) |
| 499 | .setTitle(R.string.edit_item) |
| 500 | .setView(mDialogView) |
| 501 | .setPositiveButton(R.string.dialog_ok, new DialogInterface.OnClickListener() { |
| 502 | public void onClick(DialogInterface dialog, int whichButton) { |
| 503 | /* User clicked OK so do some stuff */ |
| 504 | ContentValues values = new ContentValues(); |
| 505 | values.put(ShoppingList.ITEM, mDialogEdit.getText().toString()); |
| 506 | getContentResolver().update(mUri, values, null, null); |
| 507 | } |
| 508 | }) |
| 509 | .setNegativeButton(R.string.dialog_cancel, new DialogInterface.OnClickListener() { |
| 510 | public void onClick(DialogInterface dialog, int whichButton) { |
| 511 | /* User clicked cancel so do some stuff */ |
| 512 | } |
| 513 | }) |
| 514 | .create(); |
| 515 | case DIALOG_DELETE: |
| 516 | mDialogView = factory.inflate(R.layout.dialog_confirm, null); |
| 517 | mDialogText = (TextView)mDialogView.findViewById(R.id.dialog_confirm_prompt); |
| 518 | return new AlertDialog.Builder(this) |
| 519 | .setTitle(R.string.delete_item) |
| 520 | .setView(mDialogView) |
| 521 | .setPositiveButton(R.string.dialog_ok, new DialogInterface.OnClickListener() { |
| 522 | public void onClick(DialogInterface dialog, int whichButton) { |
| 523 | /* User clicked OK so do some stuff */ |
| 524 | getContentResolver().delete(mUri, null, null); |
| 525 | } |
| 526 | }) |
| 527 | .setNegativeButton(R.string.dialog_cancel, new DialogInterface.OnClickListener() { |
| 528 | public void onClick(DialogInterface dialog, int whichButton) { |
| 529 | /* User clicked cancel so do some stuff */ |
| 530 | } |
| 531 | }) |
| 532 | .create(); |
| 533 | case DIALOG_CLEAR: |
| 534 | mDialogView = factory.inflate(R.layout.dialog_confirm, null); |
| 535 | mDialogText = (TextView)mDialogView.findViewById(R.id.dialog_confirm_prompt); |
| 536 | return new AlertDialog.Builder(this) |
| 537 | .setTitle(R.string.clear_list) |
| 538 | .setView(mDialogView) |
| 539 | .setPositiveButton(R.string.dialog_ok, new DialogInterface.OnClickListener() { |
| 540 | public void onClick(DialogInterface dialog, int whichButton) { |
| 541 | ContentValues values = new ContentValues(); |
| 542 | //Set all items status to "off list" |
| 543 | values.put(ShoppingList.STATUS, ShoppingList.OFF_LIST); |
| 544 | getContentResolver().update(getIntent().getData(), values, null, null); |
| 545 | } |
| 546 | }) |
| 547 | .setNegativeButton(R.string.dialog_cancel, new DialogInterface.OnClickListener() { |
| 548 | public void onClick(DialogInterface dialog, int whichButton) { |
| 549 | /* User clicked cancel so do some stuff */ |
| 550 | } |
| 551 | }) |
| 552 | .create(); |
| 553 | case DIALOG_RESET: |
| 554 | mDialogView = factory.inflate(R.layout.dialog_confirm, null); |
| 555 | mDialogText = (TextView)mDialogView.findViewById(R.id.dialog_confirm_prompt); |
| 556 | return new AlertDialog.Builder(this) |
| 557 | .setTitle(R.string.reset_list) |
| 558 | .setView(mDialogView) |
| 559 | .setPositiveButton(R.string.dialog_ok, new DialogInterface.OnClickListener() { |
| 560 | public void onClick(DialogInterface dialog, int whichButton) { |
| 561 | //Permanently delete all items from the list |
| 562 | getContentResolver().delete(getIntent().getData(), null, null); |
| 563 | } |
| 564 | }) |
| 565 | .setNegativeButton(R.string.dialog_cancel, new DialogInterface.OnClickListener() { |
| 566 | public void onClick(DialogInterface dialog, int whichButton) { |
| 567 | /* User clicked cancel so do some stuff */ |
| 568 | } |
| 569 | }) |
| 570 | .create(); |
| 571 | } |
| 572 | return null; |
| 573 | } |
| 574 | |
| 575 | /** |
| 576 | * Change all items marked as "in trolley" to "off list" |
| 577 | */ |
| 578 | private void checkout() { |
| 579 | Cursor c = managedQuery(getIntent().getData(), PROJECTION, null, null, |
| 580 | ShoppingList.DEFAULT_SORT_ORDER); |
| 581 | c.moveToFirst(); |
| 582 | ContentValues values = new ContentValues(); |
| 583 | values.put(ShoppingList.STATUS, ShoppingList.OFF_LIST); |
| 584 | Uri uri; |
| 585 | int status; |
| 586 | long id; |
| 587 | //loop through all items in the list |
| 588 | while (!c.isAfterLast()) { |
| 589 | status = c.getInt(c.getColumnIndex(ShoppingList.STATUS)); |
| 590 | //if the item is not in the trolley jump to the next one |
| 591 | if (status == ShoppingList.IN_TROLLEY) { |
| 592 | id = c.getLong(c.getColumnIndexOrThrow(ShoppingList._ID)); |
| 593 | uri = ContentUris.withAppendedId(getIntent().getData(), id); |
| 594 | //Update the status of this item (in trolley) to "off list" |
| 595 | getContentResolver().update(uri, values, null, null); |
| 596 | //Cleanup the list by deleting double up items that have been checked out |
| 597 | getContentResolver().delete(getIntent().getData(), |
| 598 | ShoppingList.ITEM + "='" |
| 599 | + c.getString(c.getColumnIndex(ShoppingList.ITEM)) |
| 600 | + "' AND " + ShoppingList._ID + "<>" + id |
| 601 | + " AND " + ShoppingList.STATUS + "=" + ShoppingList.OFF_LIST, |
| 602 | null); |
| 603 | } |
| 604 | c.moveToNext(); |
| 605 | } |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | |
| 610 | |
| 611 | |